﻿using FigureDraw.Diagram;
using FigureDraw.Graphic;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FigureDraw.Shapes.DataFlowShape
{
    public class MoveDataFlow : MyShape
    {
        public MoveDataFlow()
            : base()
        {

        }

        public MoveDataFlow(MyPoint startPoint, MyPoint endPoint)
            : base(startPoint, endPoint)
        {
            
        }

        public override void Draw(CommonGraphic graphic)
        {

            double angle = Math.Atan2(endPoint.Y - startPoint.Y, endPoint.X - startPoint.X) + Math.PI;
            
            double curveDegrees = 0.5;
            double curveLength = Math.Sqrt(Math.Pow(endPoint.X - startPoint.X, 2) + Math.Pow(endPoint.Y - startPoint.Y, 2)) / (2 * Math.Cos(curveDegrees));

            double topX = endPoint.X + curveLength * Math.Cos(angle + curveDegrees);
            double topY = endPoint.Y + curveLength * Math.Sin(angle + curveDegrees);

            graphic.DrawCurve(strokeColor, strokeWidth,
                startPoint,
                new MyPoint(topX, topY),
                endPoint);

            DrawArrow(graphic, new MyPoint(topX, topY), endPoint, strokeColor, strokeWidth);
        }

        public override MyShape Clone(BlockDiagram diagram)
        {
            MyShape shape = diagram.DrawMove(startPoint, endPoint);
            shape.StrokeColor = strokeColor;
            shape.StrokeWidth = strokeWidth;
            shape.BrushColor = brushColor;

            return shape; 
        }

        private void DrawArrow(CommonGraphic graphic, MyPoint p1, MyPoint p2, MyColorInfo strokeColor, double strokeWidth)
        {
            double arrowDegrees = 0.5;
            double arrowLength = 15;

            double angle = Math.Atan2(p2.Y - p1.Y, p2.X - p1.X) + Math.PI;

            double arrowX1 = p2.X + arrowLength * Math.Cos(angle - arrowDegrees);
            double arrowY1 = p2.Y + arrowLength * Math.Sin(angle - arrowDegrees);
            double arrowX2 = p2.X + arrowLength * Math.Cos(angle + arrowDegrees);
            double arrowY2 = p2.Y + arrowLength * Math.Sin(angle + arrowDegrees);

            graphic.DrawLine(new MyPoint(arrowX1, arrowY1), p2, strokeColor, strokeWidth);
            graphic.DrawLine(new MyPoint(arrowX2, arrowY2), p2, strokeColor, strokeWidth);
        }

        public override MyShape Clone()
        {
            MyShape shape = new MoveDataFlow(startPoint, endPoint);
            shape.StrokeColor = strokeColor;
            shape.StrokeWidth = strokeWidth;
            shape.BrushColor = brushColor;

            return shape;
        }
    }
}
